#define GLOBAL_CPP
#include "global.h"
#include <QCryptographicHash>
#include <QtNetwork/QNetworkInterface>
#include <QSettings>

static const int magic1 = 0x4A8F9C6E;
static const int magic2 = 0x0058CF79;

quint32 generateUserId()
{
    QList<QNetworkInterface> netlist = QNetworkInterface::allInterfaces();
    QByteArray mac;
    quint64 id = QByteArray(DEFAULT_MAC).toULongLong(0, 16);
    foreach (QNetworkInterface net, netlist) {
        mac = net.hardwareAddress().remove(':').toLatin1();
        id += mac.toULongLong(0, 16);
    }

    id = (id^magic1) + magic2;

    return id;
}

qint32 analyzeDaysSerial(quint64 serial)
{
    QSettings settings;
    long count = 0;
    ulong uid = settings.value(USER_ID).toInt();
    quint32 temp2 = uid*magic1+magic2;
    qint32 temp1 = (serial>>16)^temp2;
    temp1 -= magic2;
    if ((temp1%17) == 0) {
        count = temp1/17;
    }
    return count;
}

qint32 getDaysCount()
{
    QSettings settings;
    qint32 count = 0;
    quint64 num = settings.value(EXPIRE_DATE, 0).toULongLong();
    qint32 temp1 = (num>>16)^magic1;
    if (((temp1*magic2)&0xFFFF) == (num&0xFFFF)) {
        if ((temp1-magic2)%17 == 0)
            count = (temp1-magic2)/17;
    }

    return count;
}

void setDaysCount(int count)
{
    QSettings settings;

    quint64 temp1 = count*17+magic2;
    quint64 encrypted = ((temp1^magic1)<<16) + ((temp1*magic2)&0xFFFF);
    settings.setValue(EXPIRE_DATE, encrypted);
}

QString encryptPassword(const QString &pwd)
{
    return QCryptographicHash::hash(pwd.toLatin1(), QCryptographicHash::Md5);
}

QString sex2trSex(const QString &sex)
{
    if (sex.contains('M', Qt::CaseInsensitive)) return QObject::tr("M");
    else if (sex.contains('F', Qt::CaseInsensitive)) return QObject::tr("F");
    else return QObject::tr("O");
}

QString trSex2Sex(const QString &trsex)
{
    if (trsex == QObject::tr("M")) return QString("M");
    else if (trsex == QObject::tr("F")) return QString("F");
    else return QString("O");
}

QTime formatDicomTime(const QString &timeStr)
{
    if (timeStr.contains('.')) {
        return QTime::fromString(timeStr, "hhmmss.zzz");
    } else {
        return QTime::fromString(timeStr, TIME_DICOM_FORMAT);
    }
}

StringTables::StringTables()
{
    GroupPermissionStringTable << QObject::tr("Examine Study") <<
               QObject::tr("Send Study") <<
               QObject::tr("Remove Study") <<
               QObject::tr("Print Image") <<
               QObject::tr("Create Report") <<
               QObject::tr("Modify Report") <<
               QObject::tr("Verify Report") <<
               QObject::tr("Print Report") <<
               QObject::tr("Import Study") <<
               QObject::tr("Export Study") <<
               QObject::tr("Register Study") <<
               QObject::tr("Modify Study") <<
               QObject::tr("Acquisit Image") <<
               QObject::tr("Local Settings") <<
               QObject::tr("Remote Servers") <<
               QObject::tr("Procedure Parameters") <<
               QObject::tr("User/Group Management") <<
               QObject::tr("Database Operation") <<
               QObject::tr("Modify Diagnostic Template");

    ScpTypeStringTable << QObject::tr("Query/Retrieve") <<
               QObject::tr("Storage") <<
               QObject::tr("Printer") <<
               QObject::tr("Worklist");

    BodyPartStringTable <<
               QObject::tr("Gastro") <<
               QObject::tr("Head") <<
               QObject::tr("Neck") <<
               QObject::tr("Chest") <<
               QObject::tr("Lung") <<
               QObject::tr("Shoulder") <<
               QObject::tr("Waist") <<
               QObject::tr("Elbow") <<
               QObject::tr("Pelvis") <<
               QObject::tr("Hand") <<
               QObject::tr("Knee") <<
               QObject::tr("Foot");

    BodyTypeStringTable <<
               QObject::tr("Normal") <<
               QObject::tr("Infant") <<
               QObject::tr("Slim") <<
               QObject::tr("Overweight");

    BodyPositionStringTable <<
               QObject::tr("PA") <<
               QObject::tr("AP") <<
               QObject::tr("LAT");

    EventTypeStringTable << QObject::tr("All Type") <<
               QObject::tr("System Info") <<
               QObject::tr("System Warn") <<
               QObject::tr("System Error") <<
               QObject::tr("Insert Study") <<
               QObject::tr("Remove Study") <<
               QObject::tr("Update Study") <<
               QObject::tr("Import Study") <<
               QObject::tr("Export Study") <<
               QObject::tr("Send Study") <<
               QObject::tr("Exposure") <<
               QObject::tr("Insert Image") <<
               QObject::tr("Print Image") <<
               QObject::tr("Send Image") <<
               QObject::tr("Remove Image") <<
               QObject::tr("Import Image") <<
               QObject::tr("Export Image") <<
               QObject::tr("Update Image") <<
               QObject::tr("Create Report") <<
               QObject::tr("Print Report") <<
               QObject::tr("Remove Report") <<
               QObject::tr("Import Report") <<
               QObject::tr("Export Report") <<
               QObject::tr("Update Report");

    LogPeriodStringTable << QObject::tr("One Week") <<
                            QObject::tr("One Month") <<
                            QObject::tr("One Year") <<
                            QObject::tr("Always");
}

const char *DetectorTypeStringTable[] = {
    " ",
    "Samsung 1717SCG",
    "CareRay 1500L",
};

const char *GeneratorTypeStringTable[] = {
    " ",
    "YiJu",
    "CPI",
    "EMD",
    "Sedecal",
};

static int i = qRegisterMetaType<WorklistItem>("WorklistItem");
